<!-- @format -->

<template>
	<div class="ma-content-block lg:flex justify-between p-4">
		<!-- CRUD 组件 -->
		<ma-crud :options="options" :columns="columns" ref="crudRef">
			<template #dept_id="{ record }">
				<a-tag color="blue">{{ record?.dept?.name }}</a-tag>
			</template>

			<template #contract_id="{ record }">
				{{ record?.contract?.name }}
			</template>

			<template #goods_id="{ record }">
				{{ record?.goods?.name }}
			</template>

			<template #warehouse_id="{ record }">
				{{ record?.warehouse?.name }}
			</template>
		</ma-crud>
	</div>
</template>
<script setup>
	import { ref, reactive, defineProps, onMounted } from "vue";
	import systemUserContractWarehouse from "@/api/system/systemUserContractWarehouse";
	import { Message } from "@arco-design/web-vue";
	import tool from "@/utils/tool";
	import * as common from "@/utils/common";
	const crudRef = ref();
	const inforow = ref();
	const dept_ids = ref();

	const props = defineProps({
		deptIds: {
			default: "",
		},
	});

	dept_ids.value = props.deptIds.dept_id;
	inforow.value = props.deptIds;

	const numberOperation = (newValue, id, numberName) => {
		systemUserContractWarehouse
			.numberOperation({ id, numberName, numberValue: newValue })
			.then((res) => {
				res.success && Message.success(res.message);
			})
			.catch((e) => {
				console.log(e);
			});
	};

	const switchStatus = (statusValue, id, statusName) => {
		systemUserContractWarehouse
			.changeStatus({ id, statusName, statusValue })
			.then((res) => {
				res.success && Message.success(res.message);
			})
			.catch((e) => {
				console.log(e);
			});
	};

	const options = reactive({
		id: "flyos_system_user_contract_warehouse",
		rowSelection: {
			showCheckedAll: true,
		},
		pk: "id",
		operationColumn: true,
		expandSearch: false,
		operationWidth: 160,
		formOption: {
			viewType: "modal",
			width: 600,
		},
		api: systemUserContractWarehouse.getList,
		recycleApi: systemUserContractWarehouse.getRecycleList,
		add: {
			show: true,
			api: systemUserContractWarehouse.save,
			auth: ["system:userContractWarehouse:save"],
		},
		edit: {
			show: true,
			api: systemUserContractWarehouse.update,
			auth: ["system:userContractWarehouse:update"],
		},
		delete: {
			show: true,
			api: systemUserContractWarehouse.deletes,
			auth: ["system:userContractWarehouse:delete"],
			realApi: systemUserContractWarehouse.realDeletes,
			realAuth: ["system:userContractWarehouse:realDeletes"],
		},
		recovery: {
			show: true,
			api: systemUserContractWarehouse.recoverys,
			auth: ["system:userContractWarehouse:recovery"],
		},
		import: {
			show: true,
			url: "system/userContractWarehouse/import",
			templateUrl: "system/userContractWarehouse/downloadTemplate",
			auth: ["system:userContractWarehouse:import"],
		},
		export: {
			show: true,
			url: "system/userContractWarehouse/export",
			auth: ["system:userContractWarehouse:export"],
		},
		beforeRequest: (params) => {
			params.dept_id = dept_ids.value
		}
	});

	const columns = reactive([
		{
			title: "",
			dataIndex: "id",
			formType: "input",
			addDisplay: false,
			editDisplay: false,
			hide: true,
			commonRules: {
				required: true,
				message: "请输入",
			},
		},
		{
			title: "合同公司",
			dataIndex: "dept_id",
			addDefaultValue: async function () {
				return inforow.value.dept_id;
			},
			disabled: true,
			align: "center",
			formType: "select",
			search: true,
			dict: {
				// 远程通用接口请求，新版代码生成都有一个 remote 接口
				remote: "system/dept/remote",
				// 指定组件接收的props
				props: { label: "name", value: "id" },
				// 开启分页
				openPage: false,
				// 远程请求配置项
				remoteOption: {
					// 按用户名排序
					sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
					// 设置查询的字段
					select: ["id", "name"],
					// 设置数据过滤as
					filter: {
						type: ["=", 2],
						status: ['=',1]
					},
					// 关联模型
					relations: [],
				},
			},
		},
		{
			title: "合同名称",
			dataIndex: "contract_id",
			align: "center",
			addDefaultValue: async function () {
				return inforow.value.id;
			},
			disabled: true,
			formType: "select",
			search: true,
			dict: {
				// 远程通用接口请求，新版代码生成都有一个 remote 接口
				remote: "system/userContract/remote",
				// 指定组件接收的props
				props: { label: "name", value: "id" },
				// 开启分页
				openPage: false,
				// 远程请求配置项
				remoteOption: {
					// 按用户名排序
					sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
					// 设置查询的字段
					select: ["id", "name"],
					// 设置数据过滤
					filter: {},
					// 关联模型
					relations: [],
				},
			},
			commonRules: {
				required: true,
				message: "请选择合同名称",
			},
		},
		{
			title: "品名",
			dataIndex: "goods_id",
			align: "center",
			formType: "select",
			search: true,
			dict: {
				// 远程通用接口请求，新版代码生成都有一个 remote 接口
				remote: `system/userContractGoods/remote?dept_id=` + dept_ids.value,
				// 指定组件接收的props
				props: { label: "name", value: "goods_id" },
				// 开启分页
				openPage: false,
				// 远程请求配置项
				remoteOption: {
					// 按用户名排序
					sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
					// 设置查询的字段
					select: ["id", "goods_id"],
					// 设置数据过滤
					filter: {
						id: ["=", dept_ids.value],
					},
				},
			},
			commonRules: {
				required: true,
				message: "请输入品名",
			},
		},
		// {
		// 	title: "仓库类型",
		// 	dataIndex: "warehouse_id",
		// 	align: "center",
		// 	formType: "select",
		// 	dict: {
		// 		// 远程通用接口请求，新版代码生成都有一个 remote 接口
		// 		remote: "warehouse/type/remote",
		// 		// 指定组件接收的props
		// 		props: { label: "name", value: "id" },
		// 		// 开启分页
		// 		openPage: false,
		// 		// 远程请求配置项
		// 		remoteOption: {
		// 			// 按用户名排序
		// 			sort: { id: "desc" }, // 如果不指定排序方式，默认为正序排序
		// 			// 设置查询的字段
		// 			select: ["id", "name"],
		// 			// 设置数据过滤
		// 			filter: {},
		// 			// 关联模型
		// 			relations: [],
		// 		},
		// 	},
		// 	commonRules: {
		// 		required: true,
		// 		message: "请选择仓库类型",
		// 	},
		// },
		{
			title: "仓位",
			dataIndex: "warehouse_id",
			align: "center",
			formType: "select",
			dict: {
				// 远程通用接口请求，新版代码生成都有一个 remote 接口
				remote: "warehouse/master/remote",
				// 指定组件接收的props
				props: { label: "name", value: "id" },
				// 开启分页
				openPage: false,
				// 定义联动，dataA的数据改变，dataB和dataC的数据也会变化
			},
			cascaderItem: [
				"horizontal_position",
				"release_position",
				"reaches_position",
			],
			commonRules: {
				required: true,
				message: "请选择仓库位置",
			},
		},
		{
			title: "仓位（横）",
			dataIndex: "horizontal_position",
			align: "center",
			formType: "select",
			dict: {
				url: "warehouse/master/getNumberList?type=1&warehouse_master_id={{key}}",
				props: { label: "name", value: "id" },
			},
			commonRules: {
				required: true,
				message: "请选择仓位（横）",
			},
		},
		{
			title: "仓位（纵）",
			dataIndex: "release_position",
			formType: "select",
			dict: {
				url: "warehouse/master/getNumberList?type=2&warehouse_master_id={{key}}",
				props: { label: "name", value: "id" },
			},
		},
		{
			title: "仓位（层）",
			dataIndex: "reaches_position",
			formType: "select",
			dict: {
				url: "warehouse/master/getNumberList?type=3&warehouse_master_id={{key}}",
				props: { label: "name", value: "id" },
			},
		},
		{
			title: "创建时间",
			dataIndex: "created_at",
			formType: "date",
			search: true,
			addDisplay: false,
			editDisplay: false,
			hide: true,
			sortable: {
				sortDirections: ["ascend", "descend"],
				sorter: true,
			},
			showTime: true,
		},
		{
			title: "更新时间",
			dataIndex: "updated_at",
			formType: "date",
			addDisplay: false,
			editDisplay: false,
			hide: true,
			showTime: true,
		},
		{
			title: "删除时间",
			dataIndex: "deleted_at",
			formType: "date",
			addDisplay: false,
			editDisplay: false,
			hide: true,
			showTime: true,
		},
		{
			title: "更新者",
			dataIndex: "updated_by",
			formType: "input",
			addDisplay: false,
			editDisplay: false,
			hide: true,
		},
		{
			title: "创建者",
			dataIndex: "created_by",
			formType: "input",
			addDisplay: false,
			editDisplay: false,
			hide: true,
		},
	]);
	defineExpose({ open });
</script>
<script>
	export default { name: "system:userContractWarehouse" };
</script>
